package com.autumn.platform.dao.schema.statement.original;

import java.util.Properties;

import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.session.Configuration;

import com.autumn.platform.dao.schema.parser.SchemaSqlMapperParserDelegate;
import com.autumn.platform.dao.schema.statement.StatementHandlerSupport;

public class CacheStatementHandler extends StatementHandlerSupport {

    @Override
    public void handleStatementNode(Configuration configuration, SchemaSqlMapperParserDelegate delegate, XNode node) {
        String type = node.getStringAttribute("type", "PERPETUAL");
        Class<? extends Cache> typeClass = resolveAlias(configuration, type);
        String eviction = node.getStringAttribute("eviction", "LRU");
        Class<? extends Cache> evictionClass = resolveAlias(configuration, eviction);
        Long flushInterval = node.getLongAttribute("flushInterval");
        Integer size = node.getIntAttribute("size");
        boolean readWrite = !node.getBooleanAttribute("readOnly", false);
        boolean blocking = node.getBooleanAttribute("blocking", false);
        Properties props = node.getChildrenAsProperties();
        delegate.getBuilderAssistant().useNewCache(typeClass, evictionClass, flushInterval, size, readWrite, blocking, props);
    }
}
